#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Author  : hu_cl
# @Contact : 760730895@qq.com 
# @Date    : 2020/7/20 9:51
# @File    : venuscloud.py
import json
import os
import sched
import time
from contextlib import closing

import requests

s = sched.scheduler(time.time, time.sleep)
# 爬取文件地址
path = 'https://backend.venuscloud.cn/upgradecenter/main/getLibById'
headers = {
    "User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) "
                  "Chrome/53.0.2785.143 Safari/537.36"
}


class ProgressBar(object):
    def __init__(self, title,
                 count=0.0,
                 run_status=None,
                 fin_status=None,
                 total=100.0,
                 unit='', sep='/',
                 chunk_size=1.0):
        super(ProgressBar, self).__init__()
        self.info = "【%s】%s %.2f %s %s %.2f %s"
        self.title = title
        self.total = total
        self.count = count
        self.chunk_size = chunk_size
        self.status = run_status or ""
        self.fin_status = fin_status or " " * len(self.status)
        self.unit = unit
        self.seq = sep

    def __get_info(self):
        # 【名称】状态 进度 单位 分割线 总数 单位
        _info = self.info % (self.title, self.status,
                             self.count/self.chunk_size, self.unit, self.seq, self.total/self.chunk_size, self.unit)
        return _info

    def refresh(self, count=1, status=None):
        self.count += count
        # if status is not None:
        self.status = status or self.status
        end_str = "\r"
        if self.count >= self.total:
            end_str = '\n'
            self.status = status or self.fin_status
        print(self.__get_info(), end=end_str)


def get_url(url):
    data_json = {"libId": 33, "modelId": 23, "page": 1, "pageSize": 20}
    web_data = requests.post(url=url, headers=headers, json=data_json)
    json_data = json.loads(web_data.text)
    datas = json_data['data']
    dicts = {}
    for num in range(2, json_data['totalPage']+1):
        data_json['page'] = num
        web_data = requests.post(url=url, headers=headers, json=data_json)
        json_data_num = json.loads(web_data.text)
        for data in json_data_num['data']:
            datas.append(data)
    for dict in datas:
        dicts[dict['title']] = dict['downloadLink']
    return dicts


def download_data(total_url):
    for (key, value) in total_url.items():
        filename = 'E:/test/天镜脆弱性扫描与管理系统V6075_' + key[18:] + ".zip"
        if not os.path.isfile(filename):
            with closing(requests.get(value, headers, stream=True)) as response:
                chunk_size = 1024  # 单次请求最大值
                # 存储路径
                file_path = 'E:/test'
                if not os.path.exists(file_path):
                    os.makedirs(file_path)
                content_size = int(response.headers['content-length'])  # 内容体总大小
                progress = ProgressBar(filename, total=content_size,
                                       unit="KB", chunk_size=chunk_size, run_status="正在下载", fin_status="下载完成")
                with open(file=filename, mode='wb') as file:
                    for data in response.iter_content(chunk_size=chunk_size):
                        file.write(data)
                        progress.refresh(count=len(data))


def main(path):
    data = get_url(path)
    download_data(data)


if __name__ == '__main__':
    delay = 2592000  # 一个月
    s.enter(delay, 2, main(path))
    s.run()
